<?php

namespace app\models;

use think\Model;

/**
 * Class CampusModel.
 */
class AllocationOnlyModel extends Model
{
    protected $table = 'edu_performance_allocation_only';

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     * @return string
     */
    public static function getIds()
    {
        $campus = self::select()->toArray();
        $arr = [];
        foreach ($campus as $cap) {
            $arr[] = $cap['id'];
        }

        return implode(',', $arr);
    }

    /**
     * @param $id
     * @param string $type
     *
     * @throws \think\exception\DbException
     *
     * @return null|array|AuthGroupAccessModel
     */
    public static function getById($id, $type = 'all')
    {
        if (!is_numeric($id)) {
            return [];
        }
        $campus = self::get(['id' => $id]);
        if ($campus) {
            return (isset($campus[$type])) ? $campus[$type] : $campus;
        }

        return [];
    }

    /**
     * @param $param
     *
     * @throws \think\exception\DbException
     *
     * @return array
     */
    public function getOnlyAllocationLists($param,$status)
    {
        $page = isset($param['page']) ? (int) $param['page'] : 1;
        $listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
        $where = [];
        if (isset($param['payee_only_text']) && '' !== $param['payee_only_text']) {
            $where['payee_only_text'] = ['like', '%'.$param['payee_only_text'].'%'];
        }
        //时间段
        if (isset($param['start_date']) && '' !== $param['start_date'] && isset($param['end_date']) && '' !== $param['end_date']) {
            $where['update_time'] = array('between', array($param['start_date'].' 00:00:00',$param['end_date'].' 23:59:59'));
        }
		if(get_current_type() == 4 || get_current_type() == 5){
			$where['campus_id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){
			$where['campus_id'] = ['in',get_current_campus_id()];
		}
		$where['status'] = 2;
		$listData = self::where($where)->order('update_time desc')
            ->paginate($listRows, false, ['page' => $page])
            ->toArray();
        $amount = 0;
        if (!empty($listData)) {
            foreach ($listData['data'] as &$item) {
                $item['rate_only'] = $item['rate_only'].'%';
                $item['rate_only_money'] =  $item['rate_only_money'] / 100;
                $amount = $amount + $item['rate_only_money'];
                $item['amount_already_only'] =  $item['amount_already_only'] / 100;

            }
        }

        if($status){
            $item_end = [];
            $item_end['contract_number'] =  '合计';
            $item_end['payee_only_text'] =  '---';
            $item_end['assignment_only_text'] =  '---';
            $item_end['rate_only'] =  '---';
            $item_end['rate_only_money'] = $amount;
            $item_end['amount_already_only'] =  '---';
            $item_end['update_time'] =  '---';
            $listData['data'][count($listData['data'])] = $item_end;
        }


        return $listData;
    }


}
